package com.xxtx.trade.notice.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xxtx.trade.common.util.IdCreateUtil;
import com.xxtx.trade.common.util.SpringUserContext;
import com.xxtx.trade.common.util.model.PageInfo;
import com.xxtx.trade.common.util.model.Result;
import com.xxtx.trade.notice.bean.InvestCallBack;
import com.xxtx.trade.notice.bean.Notice;
import com.xxtx.trade.notice.dto.AgentNoticeDto;
import com.xxtx.trade.notice.dto.NoticeDto;
import com.xxtx.trade.notice.enums.EnumsError;
import com.xxtx.trade.notice.mapper.NoticeMapper;
import com.xxtx.trade.notice.service.NoticeService;
import com.xxtx.trade.sysUser.vo.TradeSysUserVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class NoticeServiceImpl implements NoticeService {
    private static final Logger LOGGER = LoggerFactory.getLogger(NoticeServiceImpl.class);
    @Autowired
    private NoticeMapper noticeMapper;
    @Autowired
    private SpringUserContext springUserContext;

    /**
     *  查看系统的公告（带翻页）
     */
    @Override
    public Result<Object> selectNoticeList(NoticeDto noticeDto) {
        LOGGER.debug("查看系统的公告入参:{}", JSON.toJSON(noticeDto));
        Result<Object> result=null;
        try {
            result=new Result<>();
            Page page = PageHelper.startPage(noticeDto.getPageNo(), noticeDto.getPerPageNum()," create_time DESC");
            List<Notice> noticeList= noticeMapper.selectNoticeList(noticeDto);
            PageInfo pageInfo = new PageInfo();
            pageInfo.setPageNo(page.getPageNum());
            pageInfo.setPerPageNum(page.getPageSize());
            pageInfo.setTotalNum(page.getTotal());
            pageInfo.setResultList(noticeList);
            result.setData(pageInfo);
        }catch (Exception ex){
            LOGGER.error(ex.getMessage());
            result.setCode(EnumsError.NOTICE_SERVICEREAD_ERROR.getCode());
            result.setMessage(EnumsError.NOTICE_SERVICEREAD_ERROR.getDesc());
            result.setResult(Result.ERROR_MSG);
        }
        return result;
    }

    /**
     *      删除信息公告
     * @param noticeDto 要删除公告的主键id
     * @return
     */
    @Override
    public Result<Object> deleteNoticeById(NoticeDto noticeDto) {
        LOGGER.debug("删除系统的公告入参:{}", JSON.toJSON(noticeDto));
        Long deleteCount= null;
        Result<Object> result=null;
        try {
            result=new Result<>();
            deleteCount = noticeMapper.deletePublicNotice(noticeDto.getId());
        } catch (Exception e) {
            LOGGER.error(e.getMessage());
            result.setCode(EnumsError.NOTICE_SERVICEDELETE_ERROR.getCode());
            result.setMessage(EnumsError.NOTICE_SERVICEDELETE_ERROR.getDesc());
            result.setResult(Result.ERROR_MSG);
        }
        if (deleteCount<=0){
            LOGGER.error("公告 SQL删除个数为0，删除失败");
            result.setCode(EnumsError.NOTICE_SERVICEDELETE_ERROR.getCode());
            result.setMessage(EnumsError.NOTICE_SERVICEDELETE_ERROR.getDesc());
            result.setResult(Result.ERROR_MSG);
       }
        return result;
    }

    /**
     *      增加用户的信息
     * @param noticeDto
     * @return
     */
    @Override
    public Result<Object> addNotice(NoticeDto noticeDto) {
        LOGGER.debug("增加系统的公告入参:{}", JSON.toJSON(noticeDto));
        Notice notice = new Notice();
        Result result=new Result<>();
        notice.setContent(noticeDto.getContent());
        notice.setCurrentTime(new Date());
        //从缓冲中读取当前发公告人的姓名
        notice.setCreateNickname(noticeDto.getNickname());
        if (noticeDto.getTitle()==null || notice.getTitle()==""){
            LOGGER.error("系统公告标题为空");
            result.setCode(EnumsError.NOTICE_TITLENULL_ERROR.getCode());
            result.setMessage(EnumsError.NOTICE_TITLENULL_ERROR.getDesc());
            result.setResult(Result.ERROR_MSG);
            return result;
        }
        notice.setTitle(noticeDto.getTitle());
        Long insertCount= 0L;
        try {
            notice.setId(IdCreateUtil.getId());
            insertCount = noticeMapper.insertNotice(notice);
            //给代理商增加一条公告
            Long results=  noticeMapper.insertAgentNotice(notice.getId(),new Date());
        } catch (Exception e) {
            LOGGER.error(e.getMessage());
            result.setCode(EnumsError.NOTICE_SERVICEINSERT_ERROR.getCode());
            result.setMessage(EnumsError.NOTICE_SERVICEINSERT_ERROR.getDesc());
            result.setResult(Result.ERROR_MSG);
        }
        if (insertCount <= 0) {
            LOGGER.error("系统公告 SQL新增条数为0 新增失败");
            result.setCode(EnumsError.NOTICE_SERVICEINSERT_ERROR.getCode());
            result.setMessage(EnumsError.NOTICE_SERVICEINSERT_ERROR.getDesc());
            result.setResult(Result.ERROR_MSG);
        }
        return result;
    }


    //查询最新的一条公告
    @Override
    public Result<Object> newNotice() {
        LOGGER.debug("查看系统最新公告的 入参:{}");
        Result<Object> result=null;
        try {
            NoticeDto notice = new NoticeDto();
            result=new Result<>();
            Page page = PageHelper.startPage(1,1," create_time DESC");
            List<Notice> noticeList= noticeMapper.selectNoticeList(notice);
            result.setData(noticeList);
        }catch (Exception ex){
            LOGGER.error(ex.getMessage());
            result.setCode(EnumsError.NOTICE_SERVICEREAD_ERROR.getCode());
            result.setMessage(EnumsError.NOTICE_SERVICEREAD_ERROR.getDesc());
            result.setResult(Result.ERROR_MSG);
        }
        return result;

    }

    @Override
    public Result<Object> selectAgentNotice(AgentNoticeDto noticeDto) {
        LOGGER.debug("查看代理商系统的公告入参:{}", JSON.toJSON(noticeDto));
        Result<Object> result=null;
        try {
            //查询系统公告id
            result=new Result<>();
            Page page = PageHelper.startPage(noticeDto.getPageNo(), noticeDto.getPerPageNum()," create_time DESC");
            List<Notice> noticeList= noticeMapper.selectAgentNotice(noticeDto);
            PageInfo pageInfo = new PageInfo();
            pageInfo.setPageNo(page.getPageNum());
            pageInfo.setPerPageNum(page.getPageSize());
            pageInfo.setTotalNum(page.getTotal());
            pageInfo.setResultList(noticeList);
            result.setData(pageInfo);
        }catch (Exception ex){
            LOGGER.error(ex.getMessage());
            result.setCode(EnumsError.NOTICE_SERVICEREAD_ERROR.getCode());
            result.setMessage(EnumsError.NOTICE_SERVICEREAD_ERROR.getDesc());
            result.setResult(Result.ERROR_MSG);
        }
        return result;
    }

    @Override
    public Result<Object> deleteAgentNotice(AgentNoticeDto noticeDto) {
        LOGGER.debug("删除代理商系统的公告入参:{}", JSON.toJSON(noticeDto));
        Result<Object> result=null;
        try {
            //查询系统公告id
            result=new Result<>();
           long num= noticeMapper.deleteAgentNotice(noticeDto);
        }catch (Exception ex){
            LOGGER.error(ex.getMessage());
            result.setCode(EnumsError.NOTICE_SERVICEREAD_ERROR.getCode());
            result.setMessage(EnumsError.NOTICE_SERVICEREAD_ERROR.getDesc());
            result.setResult(Result.ERROR_MSG);
        }
        return result;
    }

    @Override
    public Result<Object> updateAgentNotice(AgentNoticeDto noticeDto) {
        LOGGER.debug("删除代理商系统的公告入参:{}", JSON.toJSON(noticeDto));
        Result<Object> result=null;
        try {
            //查询系统公告id
            result=new Result<>();
            long num= noticeMapper.updateAgentNotice(noticeDto);
        }catch (Exception ex){
            LOGGER.error(ex.getMessage());
            result.setCode(EnumsError.NOTICE_SERVICEREAD_ERROR.getCode());
            result.setMessage(EnumsError.NOTICE_SERVICEREAD_ERROR.getDesc());
            result.setResult(Result.ERROR_MSG);
        }
        return result;
    }

    /**
     * 查询最新一条公告
     * @param noticeDto
     * @return
     */
    @Override
    public Result<Object> selectNewAgentNotice(AgentNoticeDto noticeDto) {
        LOGGER.debug("查看系统最新公告的 入参:{}");
        Result<Object> result=new Result<>();
        Notice notice = null;
        try {
            TradeSysUserVo loginUser = springUserContext.getCurrentLoginUser();
            Integer agents= noticeMapper.selectNewNotice(loginUser.getAccountName());
           if (agents!=null && agents==2){
               result.setCode(400);
               result.setMessage("已查看");
               return  result;
           }
            notice= noticeMapper.selectNowNotices();
           result.setData(notice);
        }catch (Exception ex){
            LOGGER.error(ex.getMessage());
            result.setCode(EnumsError.NOTICE_SERVICEREAD_ERROR.getCode());
            result.setMessage(EnumsError.NOTICE_SERVICEREAD_ERROR.getDesc());
            result.setResult(Result.ERROR_MSG);
        }
        return result;
    }
}
